package org.example.pool;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.example.threads.Constant;
import org.example.threads.IDGenerator;

import java.util.concurrent.*;

/**
 * @author
 * @description
 */
public class TaskExecutor {
    /**
     * 线程池对象-单例
     */
    private static ExecutorService executorService;

    /**
     * 获取一个线程池对象
     *
     * @return 线程池对象
     */
    public static ExecutorService getExecutor() {
        if (executorService == null) {
            synchronized (TaskExecutor.class) {
                ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat(IDGenerator.getId()).build();
                executorService = new ThreadPoolExecutor(Constant.CORE_POOL_SIZE, Constant.MAXIMUM_POOL_SIZE
                        , Constant.KEEP_ALIVE_TIME_MILLIS, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), threadFactory);
            }
        }
        return executorService;
    }
}
